﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Person;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.AdminPannel
{
    /// <summary>
    /// 银行信息相关接口
    /// </summary>
    [Route("/api/v1/sysuser/personbank")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class PersonBankController : ControllerBase
    {
        private readonly PersonBankService personBankService;
        private readonly ManagementApplicationContext applicationContext;
        private readonly CorpService corpService;
        private readonly ProjectService projectService;
        private readonly ProjectGroupService projectGroupService;
        private readonly PersonService personService;
        private readonly PersonCorpContractService personCorpContractService;
        private readonly ProjectGroupMemberService projectGroupMemberService;
        private readonly IMapper mapper;
        private readonly PersonBankOwnAuthorizer ownAuthorizer;

        public PersonBankController(PersonBankService personBankService, ManagementApplicationContext applicationContext, IMapper mapper, PersonCorpContractService personCorpContractService, PersonService personService, ProjectGroupService projectGroupService, ProjectService projectService, CorpService corpService, ProjectGroupMemberService projectGroupMemberService, PersonBankOwnAuthorizer ownAuthorizer)
        {
            this.personBankService = personBankService;
            this.applicationContext = applicationContext;
            this.mapper = mapper;
            this.personCorpContractService = personCorpContractService;
            this.projectGroupService = projectGroupService;
            this.projectService = projectService;
            this.corpService = corpService;
            this.personService = personService;
            this.projectGroupMemberService = projectGroupMemberService;
            this.ownAuthorizer = ownAuthorizer;
        }

        /// <summary>
        /// 新增银行信息
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        public ApiResult CreatePersonBank(PersonBankEditForm form)
        {
            PersonBank entity = new();
            mapper.Map(form, entity);
            entity.CreateTime = DateTime.Now;
            personBankService.CreateOrUpdate(entity);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 编辑银行信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public ApiResult UpdatePersonBank(int id, PersonBankEditForm form)
        {
            PersonBank? data = personBankService.GetPersonBank(id);
            if (data == null) return ApiResult.Failure("银行信息不存在");

            #region 验证数据权限

            ownAuthorizer.CheckAndThrowIfOwnAuthenticationFailed(id);

            #endregion 验证数据权限

            mapper.Map(form, data);
            personBankService.CreateOrUpdate(data);
            return ApiResult.Success("修改成功");
        }

        /// <summary>
        /// 银行信息表单数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="corpId"></param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<PersonBankEditForm> GetPersonBank(int id, int corpId)
        {
            PersonBank? data = personBankService.GetPersonBank(id);
            if (data == null)
                return ApiResult<PersonBankEditForm>.Failure("银行信息不存在");
            return ApiResult<PersonBankEditForm>.Success(data.AsForm(corpId));
        }

        /// <summary>
        /// 获取银行信息列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<PersonBankDto>> GetPersonBanks([FromQuery] PersonBankQuery query, int pageNum = 1, int pageSize = 20)
        {
            var data = personBankService.GetPersonBanks(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsPersonBankDto(); });
            return ApiResult<PageDto<PersonBankDto>>.Success(dtos);
        }
    }
}